package com.example.myapplication.xiaochengxu;


import com.example.myapplication.shortvideo.utils.GsonUtil;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

/**
 * @author: rongfeng
 * @date: 2021/9/16
 * @description
 */
public class Quanyunhui2 {
    List<Bean> mList = new ArrayList<>();

    public static void main(String[] args) {
//        Quanyunhui2 quanyunhui = new Quanyunhui2();
//        quanyunhui.query();

        new Timer().schedule(new TimerTask() {
            @Override
            public void run() {
                try {
                    Quanyunhui2 quanyunhui = new Quanyunhui2();
                    quanyunhui.query();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }, 0, 60 * 1000);
    }

    public void query() {
        String url = "http://m.xa.bendibao.com/news/quanyunhui/winner_list.php";
        OkHttpClient okHttpClient = new OkHttpClient();
        final Request request = new Request.Builder()
//                .addHeader("Accept-Encoding", "gzip, deflate, br")
                .url(url)
                .get()//默认就是GET请求，可以不写
                .build();
        Call call = okHttpClient.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
//                String str = new String(response.body().bytes(), "GB2312");
                String str = response.body().string();
                mList.clear();

                String tag1 = "<div class=\"winner_list_container\">";
                int index1 = str.indexOf(tag1);
                String html = str.substring(index1 + tag1.length());
                String tag2 = " </div>";
                int index2 = html.indexOf(tag2);
                String html2 = html.substring(0, index2);
                fenxi(html2);
                save();
            }
        });
    }

    public void save() {
        String json = GsonUtil.toJson(mList);
        try {
            File file = new File("D:\\downloads\\quanyunhui\\quanyunhui3.json");

            //if file doesnt exists, then create it
            if (file.exists()) {
                file.delete();
            }
            file.createNewFile();

            FileWriter fw = new FileWriter(file.getAbsoluteFile());
//            OutputStreamWriter fw = new OutputStreamWriter(new FileOutputStream(file),"GB2312");

            BufferedWriter bw = new BufferedWriter(fw);
            bw.write(json.replace("\\r\\n","").replace(" ",""));
            bw.close();

            System.out.println("保存成功");

        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    private void fenxi(String html) {
        String tag1 = "<tr>";
        int index1 = html.indexOf(tag1);
        if (index1 > 0) {
            Bean bean = new Bean();
            String html2 = html.substring(index1 + tag1.length());


            for (int i = 0; i < 4; i++) {

                String tag2 = "<td>";
                int index2 = html2.indexOf(tag2);
                String html3 = html2.substring(index2 + tag2.length());

                String tag3 = "</td>";
                int index3 = html3.indexOf(tag3);
                String html4 = html3.substring(0, index3);

                if (i == 0) {
                    bean.gameName = html4;
                } else if (i == 1) {
                    bean.gameTeam = html4;
                } else if (i == 2) {
                    int medal = 0;
                    if (html4.contains("first")) {
                        medal = 0;
                    } else if (html4.contains("second")) {
                        medal = 1;
                    } else if (html4.contains("third")) {
                        medal = 2;
                    }
                    bean.medal = medal;
                } else if (i == 3) {
                    bean.time = html4;
                }
                html2 = html3;
            }
            mList.add(bean);
            fenxi(html2);
        }
    }

    public static class Bean implements Serializable {
        public String gameName;
        public String gameTeam;
        public int medal;
        public String time;
    }
}
